package me.shurufa.activities;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.LogUtils;
import me.shurufa.utils.StatusBarCompat;
import me.shurufa.utils.Utils;
import me.shurufa.utils.ViewUtil;

/**
 * BaseActivity基类
 *
 * @FileName: me.shurufa.activities.BaseAppCompatActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-14 15:40
 */
public class BaseActivity extends AppCompatActivity {
    protected int activity_layout_res;

    @Bind(R.id.tool_bar)
    @Nullable
    LinearLayout toolbar;

    @Bind(R.id.toolbar_title)
    @Nullable
    TextView titleTextView;

    @Bind(R.id.back)
    @Nullable
    View backInToolbar;

    View rootView;
    //public SwipeBackLayout mSwipeBackLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置横向滑动
        // mSwipeBackLayout = getSwipeBackLayout();
        // mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        setTheme(R.style.AppTheme);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        if (activity_layout_res != 0) {
            setContentView(activity_layout_res);
        } else {
            finish();
        }
        initStatusBar();
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        if (backInToolbar != null)
            backInToolbar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    InputMethodManager inputMethodManager = (InputMethodManager) BaseActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    onBackPressed();
                }
            });
        rootView = getWindow().getDecorView().findViewById(android.R.id.content);

        // debug模式显示push栈的Activity的name
        LogUtils.i(LogUtils.tag, "Push Stack: " + this.getClass().getName());
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!Global.DEBUG) {
            MobclickAgent.onResume(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (!Global.DEBUG) {
            MobclickAgent.onPause(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        ViewUtil.fixInputMethodManagerLeak(this);
        // debug模式显示pop栈的Activity的name
        LogUtils.i(LogUtils.tag, "Pop Stack: " + this.getClass().getName());
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        try {
            super.onSaveInstanceState(outState);
        } catch (Exception e) {
        }
    }

    public void onEventMainThread(Intent intent) {
        /*if (intent.getAction().equals(Constants.ACTION_USER_CHANGE)) {
            if (!(this instanceof LoginActivity) || !(this instanceof SettingsActivity)) {
                finish();
            }
        } else */
        if (intent.getAction().equals(Constants.ACTION_GO_LOGIN)) {
            if (!(this instanceof LoginActivity)) {
                Utils.showToast("登陆后即可使用此功能");
                Intent loginIntent = new Intent(BaseActivity.this, NewLoginActivity.class);
                startActivity(loginIntent);
                finish();
            }
        }
    }

    /**
     * 可重写
     */
    public void initStatusBar() {
        StatusBarCompat.setColor(this, getResources().getColor(R.color.red_new), 0);
    }

}
